﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MaxLeaf.Platform.Contract;
using MaxLeaf.Platform.BLLImplementation;
using CommonsLib.Utility;
using MaxLeaf.Platform.DataContract;
using CommonsLib.Settings;
using MaxLeaf.Platform.Functions;

namespace MaxLeaf.Platform.BLLProxy
{
    /// <summary>
    /// 基本数据操作，查询，添加，修改，删除
    /// </summary>
    public class MtnBLLProxy : BaseBLLProxy, IMtn
    {
        public   IMtn CurrentBLL
        {
            get;
            set;
        }
        
        public MtnBLLProxy():base()
        {
            switch ( Access_Mode )
            {
                
                case MaxLeaf.Platform.Commons.Const.ACCESS_Direct:
                    {
                        CurrentBLL = new MtnBLL();
                        break;
                    }

                case MaxLeaf.Platform.Commons.Const.ACCESS_IIS:
                    {
                        CurrentBLL = new MtnBLLWCFClient(this.WCF_Configs);
                        break;
                    }


                default:
                    {
                        throw new MaxLeaf.Platform.Commons.MaxLeafException(MaxLeaf.Platform.Commons.Const.EXCEPTION_MESSAGE_INVALID_ACCESSMODE_CONFIG);

                    }
            }
        }



        
        /// <summary>
        /// 按 主键Key 查找 单行数据
        /// </summary>
        /// <param name="model">实体类空对象，用来告诉程序需要的实体类类型</param>
        /// <param name="intKeyValue">Key值	</param>
        /// <returns>填充数据的实体类</returns>
        public object Select(object model, int intKeyValue)
        {
            
                return CurrentBLL.Select(model, intKeyValue);
             
        }

        /// <summary>
        /// 按 外键ForeignKey 查找 Detail数据
        /// </summary>
        /// <param name="model">实体类空对象，用来告诉程序需要的实体类类型</param>
        /// <param name="intForeignKeyValue">ForeignKey值</param>
        /// <returns>填充数据的实体类List集合</returns>
        public List<object>  SelectDtl(object model, int intForeignKeyValue)
        {

            return CurrentBLL.SelectDtl(model, intForeignKeyValue);

        }

        /// <summary>
        /// 根据 提交的AddInfo 实现复杂查询
        /// </summary>
        /// <param name="model">实体类空对象，用来告诉程序需要的实体类类型</param>
        /// <param name="lstAddInfo">
        ///查询附加信息，包括：Cond, SortBy
        /// </param>
        /// <returns>填充数据的实体类List集合</returns>
        public SelectResult SelectByCond(object model, List<AddInfo> lstAddInfo)
        {

            return CurrentBLL.SelectByCond(model, lstAddInfo);

        }


        /// <summary>
        /// 根据 提交的AddInfo Sum数据
        /// </summary>
        /// <param name="model">实体类空对象，用来告诉程序需要的实体类类型</param>
        /// <param name="lstAddInfo">
        ///查询附加信息，包括：Cond, SumInfo
        /// </param>
        /// <returns>填充Sum数据的List集合</returns>
        public SumResult Sum(object model, List<AddInfo> lstAddInfo)
        {

            return CurrentBLL.Sum(model, lstAddInfo);

        }


        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model">填充好数据的实体类对象</param>
        /// <returns>填充数据的实体类（可能有部分值要从数据库回写）	</returns>
        public object Add(object model)
        {
            ((BaseDataModel)model).ClearDictData();
            return CurrentBLL.Add(model);
                 
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="model">已从数据库取出数据的实体类对象</param>
        /// <returns>填充数据的实体类（可能有部分值要从数据库回写）	</returns>
        public object Update(object model)
        {
            ((BaseDataModel)model).ClearDictData();
            return CurrentBLL.Update(model);
            
        }

        /// <summary>
        /// 删除，如果实体类存在is_delete则执行假删除真修改
        /// </summary>
        /// <param name="model">已从数据库取出数据的实体类对象</param>
        /// <returns>是否成功</returns>
        public bool Delete(object model)
        {
            ((BaseDataModel)model).ClearDictData();
            return CurrentBLL.Delete(model);
            
        }

        /// <summary>
        /// 更改状态变为未删除
        /// </summary>
        /// <param name="model">已从数据库取出数据的实体类对象</param>
        /// <returns>是否成功</returns>
        public bool Undelete(object model)
        {
            ((BaseDataModel)model).ClearDictData();
            return CurrentBLL.Undelete(model);

        }
    }
}
